package com.yx.cloud.activity;

import android.animation.Animator;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.iot.common.utils.LogUtils;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.bean.CountryCodeBean;
import com.yx.cloud.country.CountryActivity;
import com.yx.cloud.databinding.AccountshareActivityBinding;
import com.yx.cloud.utils.AnimationtUtils;
import com.yx.cloud.utils.CountryCodeUtils;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LanguageUtils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.common.utils.Event;
import com.iot.base.service.req.ShareDeviceParam;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 帐号分享
 */
public class AccountShareActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "AccountShareActivity";

    private String contryCode;
    private AccountshareActivityBinding binding;

    @Override
    public void onCreate() {
        binding = AccountshareActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {

//        contryCode = SharedPrefsUtil.getValue(this, Constants.USERNAME, "contryCode", "");
        contryCode = CountryCodeUtils.getCountryZipCode(this);
        String systemLanguage = LanguageUtils.getSystemLanguage(this);
        String substring = systemLanguage.substring(3, 5);
        ArrayList<CountryCodeBean> codeList = CountryCodeUtils.getCountryCodeList(this);
        for (int i = 0; i < codeList.size(); i++) {
            if (substring.equals("" + codeList.get(i).getLocale())) {
                LogUtils.i(codeList.get(i).getCode() + ": tvCountryCode :" + codeList.get(i).getName());
                contryCode = codeList.get(i).getCode() + "";
            }
        }

        binding.tvAccountShareContry.setText(contryCode);
        binding.btnAccountsShareButton.setEnabled(false);
        /**
         * 对帐号密码输入框进行监听
         */
        binding.etShareAccounts.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (!"".equals(editable.toString().trim()) && editable.length() > 1) {
                    binding.btnAccountsShareButton.setEnabled(true);
                } else {
                    binding.btnAccountsShareButton.setEnabled(false);
                    return;
                }
            }
        });
        //判断系统动画是否开启
        boolean powerSaveMode = AnimationtUtils.isPowerSaveMode(this);
        if (!powerSaveMode) {
            binding.lottieAnimationView.addAnimatorListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    binding.btnAccountsShareButton.setEnabled(true);
                    binding.lottieAnimationView.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
        }

        binding.ivAccountShareReturn.setOnClickListener(this);
        binding.btnAccountsShareButton.setOnClickListener(this);
//        binding.tvAccountShareContry.setOnClickListener(this);
        binding.llAccountShareLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_accountShareReturn:
                Intent_utils.backIntent(this);
                break;
            case R.id.btn_accountsShareButton:
                Log.d(TAG, "=========分享====== :" + 1111);
                //显示
                binding.lottieAnimationView.setVisibility(View.VISIBLE);
                binding.lottieAnimationView.playAnimation();
                binding.btnAccountsShareButton.setEnabled(false);

                String accounts = binding.etShareAccounts.getText().toString().trim();
                String deviceId = AppContext.get().getCurrentDevice().getDeviceId();
                List<String> devIdList = new ArrayList<>();
                devIdList.add(deviceId);
//                Log.d(TAG, "=========分享22222222====== :" + deviceId);
                contryCode = binding.tvAccountShareContry.getText().toString().trim();
                ShareDeviceParam param = new ShareDeviceParam();
                param.setAccount(accounts);
                param.setCountryCode(contryCode);
                param.setDevIds(devIdList);
                param.setAutoSharing(true);
                AppContext.getProductService().shareDevice(param);
                Log.d(TAG, "=========分享====== :");
                break;

            case R.id.ll_accountShareLayout:
                Intent intenta = new Intent(this, CountryActivity.class);
                startActivityForResult(intenta, 1);
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        //销毁
        binding.lottieAnimationView.setVisibility(View.GONE);
         binding.btnAccountsShareButton.setEnabled(true);
        if (event.getType() == Event.MESSAGE_TYPE_SHARE_DEVICE) {
            if (event.isResult()) {
                Log.d(TAG, "=========分享====== :");
                ToastUtil.show(AccountShareActivity.this, getString(R.string.MO_T_ShareUn_Ok), Toast.LENGTH_LONG);
                Intent_utils.backIntent(AccountShareActivity.this);
            } else {
                Log.d(TAG, "=========分享======失败 :");
                String errorMsgByCode = AppContext.getProductService().getErrorMsgByCode(this, event.getContent() + "");
                ToastUtil.show(AccountShareActivity.this, errorMsgByCode, Toast.LENGTH_LONG);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "-------------------------");

        if (requestCode == 1) {
            if (resultCode == 2) {
                String countryCode = data.getStringExtra("countryCode");
                binding.tvAccountShareContry.setText("" + countryCode);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

}
